package com.zhss.designpattern.builder;

/**
 * @author Administrator
 */

public class OptimizedBuilderPatternDemoPractice {
	public static void main(String[] args) {
		Student build = new Builder().setAge(11).setName("aa").setSex("nan").build();
		System.out.println(build.toString());
	}


	/**
	 * 待构建对象
	 */
	public static class Student{
		private String name;
		private Integer age;
		private String sex;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getAge() {
			return age;
		}

		public void setAge(Integer age) {
			this.age = age;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

		@Override
		public String toString() {
			return "Student{" +
					"name='" + name + '\'' +
					", age=" + age +
					", sex='" + sex + '\'' +
					'}';
		}
	}


	/**
	 * build的通用接口
	 */
	public interface Build{
		Build setName(String name);
		Build setAge(Integer age);
		Build setSex(String sex);
		Student build();
	}

	/**
	 * builder*,在每个方法里返回build,以便继续调用set方法，
	 * 同时set student的属性
	 *
	 *
	 */
	public  static class Builder implements Build{
		Student student=new Student();

		public Build setName(String name) {
			student.setName(name);
			return this;
		}

		public Build setAge(Integer age) {
			student.setAge(age);
			return this;
		}

		public Build setSex(String sex) {
			student.setSex(sex);
			return this;
		}

		public Student build() {
			return student;
		}
	}
}
